
import json
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from assets.models import WorkflowTemplate

class Command(BaseCommand):
    help = 'Seeds the database with initial workflow templates'

    def handle(self, *args, **options):
        self.stdout.write('Seeding workflow templates...')

        # Find an admin user to be the creator
        admin_user = User.objects.filter(is_superuser=True).first()
        if not admin_user:
            self.stdout.write(self.style.ERROR('No superuser found. Please create a superuser first.'))
            return

        # 1. Asset Borrow Workflow Template
        borrow_template_name = '资产借用审批流程'
        if not WorkflowTemplate.objects.filter(name=borrow_template_name).exists():
            borrow_definition = {
                'steps': [
                    {
                        'id': 'approval',
                        'name': '部门主管审批',
                        'type': 'manual',
                        'assignee_type': 'manager', # 'manager' or specific user/group ID
                        'reject_terminates_workflow': True
                    }
                ]
            }
            WorkflowTemplate.objects.create(
                name=borrow_template_name,
                template_type='asset_borrow',
                description='用于员工借用公司资产的审批流程。需要部门主管批准。',
                workflow_definition=borrow_definition,
                created_by=admin_user
            )
            self.stdout.write(self.style.SUCCESS(f'Successfully created "{borrow_template_name}" template.'))
        else:
            self.stdout.write(self.style.WARNING(f'Template "{borrow_template_name}" already exists.'))

        # Add other templates here if needed in the future

        self.stdout.write(self.style.SUCCESS('Workflow template seeding complete.'))
